package com.mozz.finally_work.servlet;

import com.mozz.finally_work.dao.BlogDao;
import com.mozz.finally_work.pojo.Blog;
import com.mozz.finally_work.utils.MybatisUtils;
import com.mozz.finally_work.utils.ParseMarkdown;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "UserHomeServlet", value = "/UserHomeServlet")
public class UserHomeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        判断参数
        String userId = request.getParameter("userId");
        if (userId.equals("") || userId == null) {
            response.sendRedirect("index.jsp");
            return;
        }
        // 查询用户的所有博客
        SqlSession session = MybatisUtils.getSession();
        BlogDao dao = session.getMapper(BlogDao.class);
        List<Blog> blogs = dao.queryAllBlogByUserId(userId);
        for (Blog blog : blogs) {
            // 将 content由markdown转为html
            String parse = ParseMarkdown.parse(blog.getContent());
            blog.setContent(parse);
        }
        session.close();
        // 设置request作用域参数
        request.setAttribute("blogs", blogs);
        request.getRequestDispatcher("user_home.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
